package com.xinbo.tuiyousuper.fragment;

import java.util.ArrayList;
import java.util.List;

import u.aly.v;

import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xinbo.tuiyousuper.R;
import com.xinbo.tuiyousuper.model.fg2data.Data;
import com.xinbo.tuiyousuper.model.fg2data.Destination;
import com.xinbo.tuiyousuper.model.fg2data.Element;
import com.xinbo.tuiyousuper.model.fg2data.Module;
import com.xinbo.tuiyousuper.model.fg2data.Tag;
import com.xinbo.tuiyousuper.utils.ApiClient;
import com.xinbo.tuiyousuper.utils.CustomerGridView;
import com.xinbo.utils.GsonUtils;
import com.xinbo.utils.UILUtils;
import com.xinbo.utils.VolleyListener;

import android.graphics.Color;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class Fragment2 extends Fragment
{

	private LayoutInflater inflater;
	private List<Tag> tags = new ArrayList<Tag>();
	private List<Module> modules = new ArrayList<Module>();
	private List<Element> elements = new ArrayList<Element>();
	private FristAdapter fristAdapter;
	private SecondAdapter secondAdapter;
	private ListView listView2;
	private GridAdapter gridAdapter;
	private int indexLine=0;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState)
	{
		this.inflater = inflater;
		View layout = inflater.inflate(R.layout.fragment_fragment2, container,
		        false);
		ListView listView1 = (ListView) layout.findViewById(R.id.listView1_fg2);
		listView2 = (ListView) layout.findViewById(R.id.listView2_fg2);
		fristAdapter = new FristAdapter();
		secondAdapter = new SecondAdapter();
		listView1.setAdapter(fristAdapter);
		listView2.setAdapter(secondAdapter);
		initData();
		listView1.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
			        int position, long id)
			{
				final int index = position;
				ApiClient.fg2Destination(getContext(), new VolleyListener()
				{
					@Override
					public void onResponse(String json)
					{
						indexLine=index;
						Destination destination = GsonUtils.parseJSON(json,
						        Destination.class);
						Data data = destination.getData();
						tags = data.getTags();
						Tag tag = tags.get(index);
						modules = tag.getModules();
						fristAdapter.notifyDataSetChanged();
						
						
					}
					@Override
					public void onErrorResponse(VolleyError arg0)
					{
					}
				});
			}
		});
		return layout;
	}

	private void initData()
	{
		ApiClient.fg2Destination(getContext(), new VolleyListener()
		{

			@Override
			public void onResponse(String json)
			{
				Destination destination = GsonUtils.parseJSON(json,
				        Destination.class);
				Data data = destination.getData();
				tags = data.getTags();
				Tag tag = tags.get(indexLine);
				modules = tag.getModules();
				fristAdapter.notifyDataSetChanged();
				secondAdapter.notifyDataSetChanged();
			}

			@Override
			public void onErrorResponse(VolleyError arg0)
			{
				// TODO Auto-generated method stub

			}
		});

	}

	class FristAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{

			return tags.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = inflater.inflate(R.layout.fragment2_listview_item1,
			        null);
			TextView mTextView = (TextView) layout
			        .findViewById(R.id.textView1_fg2_listview1_item);
			TextView line = (TextView) layout.findViewById(R.id.tvLine_listview1_item);
			mTextView.setText(tags.get(position).getTitle());
			if (position==indexLine)
            {
				line.setVisibility(View.VISIBLE);
				mTextView.setBackgroundColor(Color.WHITE);
            }else {
            	line.setVisibility(View.INVISIBLE);
            	
			}
			secondAdapter.notifyDataSetChanged();
			return layout;
		}

	}

	class SecondAdapter extends BaseAdapter
	{

		

		@Override
		public int getCount()
		{

			return modules.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = inflater.inflate(R.layout.fragment2_listview_item2,
			        null);
			CustomerGridView mCustomerGridView = (CustomerGridView) layout
			        .findViewById(R.id.gridView1_fg2);
			TextView tvTitle = (TextView) layout.findViewById(R.id.tv_listview2_item_fg2);
			String title = modules.get(position).getTitle();
			elements = modules.get(position).getElements();
			gridAdapter = new GridAdapter();
			if (elements != null && elements.size() > 0)
			{
				if (!"".equals(title))
	            {
		            tvTitle.setVisibility(View.VISIBLE);
		            tvTitle.setText(title);
	            }
				if (elements.size() == 1)
				{
					mCustomerGridView.setNumColumns(1);
				} else if (elements.size() > 1 && elements.size() <= 4)
				{
					mCustomerGridView.setNumColumns(2);
				} else
				{
					mCustomerGridView.setNumColumns(3);
				}
				mCustomerGridView.setAdapter(gridAdapter);
			}
			
			return layout;
		}

	}

	class GridAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			return elements.size();
		}

		@Override
		public Object getItem(int position)
		{
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = inflater.inflate(
			        R.layout.fragment2_listview_item2_gridview_item, null);
			TextView mTextView = (TextView) layout
			        .findViewById(R.id.tv_elements_title_griditem);
			TextView tvDesc = (TextView) layout
			        .findViewById(R.id.tv_elements_desc_grid);
			ImageView mImageView = (ImageView) layout
			        .findViewById(R.id.iv_elements_griditem);
			Element element = elements.get(position);
			String desc = element.getDesc();
			String imgUrl = element.getImgUrl();
			if (!"".equals(desc)) 
			{
				tvDesc.setText(desc);
				tvDesc.setVisibility(View.VISIBLE);
			}
			if (!"".equals(imgUrl))
			{
				mImageView.setVisibility(View.VISIBLE);
				UILUtils.displayImageNoAnim(imgUrl, mImageView);
				mTextView.setTextColor(Color.WHITE);
				mTextView.setTextSize(20);
				mTextView.setText(element.getTitle());
			} else
			{	
				mImageView.setVisibility(View.GONE);
				mTextView.setText(element.getTitle());
			}
			return layout;
		}

	}
	@Override
	public void onDestroyView()
	{
		ImageLoader.getInstance().clearMemoryCache();
	    super.onDestroyView();
	}
}
